package com.bangdream.michelia.model;

import com.alibaba.fastjson.JSON;
import com.bangdream.michelia.application.AppCurrentUser;
import com.bangdream.michelia.contract.ExamContract;
import com.bangdream.michelia.entity.exam.AnswerBean;
import com.bangdream.michelia.tool.retrofithttp.RetroFactory;
import com.bangdream.michelia.utils.L;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @date on 2018/8/19 14:59
 * @email 909732184@qq.com
 * 考试
 */
public class IExamModelImpl implements ExamContract.IExamModel{
    @Override
    public Observable getExamList(Map<Object, Object> paramsMap,String type) {
        String jsonString = JSON.toJSONString(paramsMap);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),jsonString);

        if(type.equals("1")){
            return RetroFactory.getInstance().getExamList(AppCurrentUser.getInstance().getToken(),
                    requestBody);
        }else {
            return RetroFactory.getInstance().getTaskExamList(AppCurrentUser.getInstance().getToken(),
                    requestBody);
        }

    }


    @Override
    public Observable getExamQuList(String type) {
        HashMap paramsMap = new HashMap<>();
        paramsMap.put("token",AppCurrentUser.getInstance().getToken());
//        {"itemType":"02","pageNo":1,"pageSize":20,"userId":"afbaecf461ab4bc6aec33a01d86f8af2"}
        paramsMap.put("itemType","02");

//        if(type!=null)
//            paramsMap.put("type",type);
        String jsonString = JSON.toJSONString(paramsMap);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),jsonString);
        return RetroFactory.getInstance().getQuestionList(AppCurrentUser.getInstance().getToken(),requestBody);
//        return RetroFactory.getInstance().getExamQuList(AppCurrentUser.getInstance().getToken(),requestBody);
    }
    @Override
    public Observable getSubjectList(String examId) {
        Map paramsMap = new HashMap();
        paramsMap.put("token",AppCurrentUser.getInstance().getToken());
        paramsMap.put("examId",examId);
        String jsonString = JSON.toJSONString(paramsMap);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),jsonString);
        return RetroFactory.getInstance().getSubjectList(AppCurrentUser.getInstance().getToken(),requestBody);
    }

    @Override
    public Observable getSubjectDetails(Map<Object, Object> paramsMap) {
        if(paramsMap==null){
            paramsMap = new HashMap<>();
        }
        paramsMap.put("token",AppCurrentUser.getInstance().getToken());
        //paramsMap.put("examId",examId);
        String jsonString = JSON.toJSONString(paramsMap);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),jsonString);
        return RetroFactory.getInstance().getSubjectDetails(AppCurrentUser.getInstance().getToken(),requestBody);
    }

    @Override
    public Observable initExamId(Map<Object, Object> paramsMap) {
        if(paramsMap==null){
            paramsMap = new HashMap<>();
        }
        paramsMap.put("token",AppCurrentUser.getInstance().getToken());
        String jsonString = JSON.toJSONString(paramsMap);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),jsonString);
        return RetroFactory.getInstance().actionInitExamId(AppCurrentUser.getInstance().getToken(),requestBody);
    }

    @Override
    public Observable actionSubmitExam(AnswerBean bean) {
        HashMap paramsMap = new HashMap<>();
        paramsMap.put("token",AppCurrentUser.getInstance().getToken());
        if(bean!=null){
            paramsMap.put("answer",bean.getAnswer());
            paramsMap.put("examId",bean.getExamId());
            paramsMap.put("examStart",bean.getExamStart());
            paramsMap.put("id",bean.getId());
            paramsMap.put("isFinish",bean.getIsFinish());
            paramsMap.put("mark",bean.getMark());
            paramsMap.put("paperId",bean.getPaperId());
            paramsMap.put("questionId",bean.getQuestionId());
            paramsMap.put("userId",bean.getUserId());
        }
        String jsonString = JSON.toJSONString(paramsMap);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),jsonString);
        return RetroFactory.getInstance().actionSubmitExam(AppCurrentUser.getInstance().getToken(),requestBody);
    }
    @Override
    public Observable actionSubmitQuest(AnswerBean bean) {
        HashMap paramsMap = new HashMap<>();
        paramsMap.put("token",AppCurrentUser.getInstance().getToken());
        if(bean!=null){
            paramsMap.put("answer",bean.getAnswer());
            paramsMap.put("examId",bean.getExamId());
            paramsMap.put("paperId",bean.getPaperId());
            paramsMap.put("userId",bean.getUserId());
        }
        String jsonString = JSON.toJSONString(paramsMap);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),jsonString);
        return RetroFactory.getInstance().actionSubmitQuest(AppCurrentUser.getInstance().getToken(),requestBody);
    }
    @Override
    public Observable getExamQusetList(String examId) {
        HashMap paramsMap = new HashMap<>();
        paramsMap.put("token",AppCurrentUser.getInstance().getToken());
        if(examId!=null)
            paramsMap.put("examId",examId);
        String jsonString = JSON.toJSONString(paramsMap);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),jsonString);
        return RetroFactory.getInstance().getExamQuestList(AppCurrentUser.getInstance().getToken(),requestBody);
    }
    @Override
    public Observable getExamResultList(String examId) {
        HashMap paramsMap = new HashMap<>();
        paramsMap.put("token",AppCurrentUser.getInstance().getToken());
        if(examId!=null)
        paramsMap.put("examId",examId);
        String jsonString = JSON.toJSONString(paramsMap);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),jsonString);
        return RetroFactory.getInstance().getExamResult(AppCurrentUser.getInstance().getToken(),requestBody);
    }

    @Override
    public Observable getExamResultDetails(String examId) {
        HashMap paramsMap = new HashMap<>();
        paramsMap.put("token",AppCurrentUser.getInstance().getToken());
        if(examId!=null)
            paramsMap.put("id",examId);
        String jsonString = JSON.toJSONString(paramsMap);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),jsonString);
        return RetroFactory.getInstance().getExamResultDetails(AppCurrentUser.getInstance().getToken(),requestBody);

    }

    @Override
    public Observable getSignUpList(int pageNo,int pageSize) {
        HashMap paramsMap = new HashMap<>();
        paramsMap.put("pageNo",pageNo);
        paramsMap.put("pageSize",pageSize);
        paramsMap.put("userId",AppCurrentUser.getInstance().getUserInfo().getId());

        String jsonString = JSON.toJSONString(paramsMap);

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),jsonString);
        return RetroFactory.getInstance().getSignUpList(AppCurrentUser.getInstance().getToken(),requestBody);
    }

    @Override
    public Observable getMyExam(Map<Object, Object> paramsMap) {
        String jsonString = JSON.toJSONString(paramsMap);

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),jsonString);
        return RetroFactory.getInstance().getMyExam(AppCurrentUser.getInstance().getToken(),requestBody);

    }
}
